#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    if ( argc != 3)
    {
        printf("Pls input: ./a.out <src file> <dst file>\n");
        return -1;
    }

    int fdSrc = open(argv[1], O_RDONLY);
    int fdDst = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0664);
    if( fdSrc == -1 || fdDst == -1)
    {
        perror("Fail open");
        return -1;
    }

    char buff[1000]={0};
    ssize_t szRead, szWrite, szCounter=0;
    while( ( szRead = read(fdSrc, buff, sizeof(buff)) ) > 0 )
    {
        szWrite = write(fdDst, buff, szRead);
        if( szWrite == -1 )
        {
            perror("Fail write");
            return -1;
        }
        szCounter += szWrite;
    }
    if( szRead == -1)
    {
        perror("Fail read");
        return -1;
    }

    printf("Total write %ld bytes from <%s> to <%s>\n", 
            szCounter, argv[1], argv[2]);
    
    close(fdDst);
    close(fdSrc);

    return 0;
}


/********************************************

系统调用函数 open read write close
shell传参给main函数
判断每一个会设置 errno 的函数返回值

********************************************/